package net.sunofbeaches.services.impl;

import net.sunofbeaches.dao.ArticleNoContentDao;
import net.sunofbeaches.dao.CommentDao;
import net.sunofbeaches.pojo.ArticleNoContent;
import net.sunofbeaches.pojo.Comment;
import net.sunofbeaches.pojo.SobUser;
import net.sunofbeaches.response.ResponseResult;
import net.sunofbeaches.services.CommentServices;
import net.sunofbeaches.services.IUserService;
import net.sunofbeaches.utils.Constants;
import net.sunofbeaches.utils.IdWorker;
import net.sunofbeaches.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import javax.xml.soap.Text;
import java.util.Date;

@Service
@Transactional
public class CommentServicesImpl extends BaseService implements CommentServices {
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private CommentDao commentDao;
    @Autowired
    private IUserService iUserService;
    @Autowired
    private ArticleNoContentDao articleNoContentDao;

    /**
     * 发表评论
     *
     * @param comment 评论
     * @return
     */
    @Override
    public ResponseResult postComment(Comment comment) {
        SobUser sobUser = iUserService.checkSobUser();
        if (sobUser == null) {
            return ResponseResult.ACCOUNT_NOT_LOGIN();
        }
        String articleId = comment.getArticleId();
        if (TextUtils.isEmpty(articleId)) {
            return ResponseResult.FAILED("文章ID不可以为空.");
        }
        ArticleNoContent article = articleNoContentDao.findOneById(articleId);
        if (article == null) {
            return ResponseResult.FAILED("文章不存在.");
        }
        String content = comment.getContent();
        if (TextUtils.isEmpty(content)) {
            return ResponseResult.FAILED("评论内容不可以为空.");
        }
        //补全内容
        comment.setId(idWorker.nextId() + "");
        comment.setUpdateTime(new Date());
        comment.setCreateTime(new Date());
        comment.setUserAvatar(sobUser.getAvatar());
        comment.setUserName(sobUser.getUserName());
        comment.setUserId(sobUser.getId());
        comment.setState("1");
        //保存入库
        commentDao.save(comment);
        /**
         * TODO : 可添加字段 然后发邮箱给被评论者
         */
        //返回结果
        return ResponseResult.SUCCESS("评论成功");
    }

    @Override
    public ResponseResult listCommentByArticleID(String articleId, int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        Sort sort = new Sort(Sort.Direction.DESC, "state", "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Comment> all = commentDao.findAll(new Specification<Comment>() {
            @Override
            public Predicate toPredicate(Root<Comment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                return criteriaBuilder.equal(root.get("articleId").as(String.class), articleId);
            }
        }, pageable);
        return ResponseResult.SUCCESS("评论列表获取成功.").setData(all);
    }

    @Override
    public ResponseResult deleteCommentById(String commentId) {
        SobUser sobUser = iUserService.checkSobUser();
        // 检查用户角色
        if (sobUser == null) {
            return ResponseResult.ACCOUNT_NOT_LOGIN();
        }
        // 把评论找出来，比对用户权限
        Comment comment = commentDao.findAllById(commentId);
        if (comment == null) {
            return ResponseResult.FAILED("评论不存在.");
        }
        // 判断登录角色
        if (sobUser.getId().equals(comment.getUserId())
                || Constants.User.ROLE_ADMIN.equals(sobUser.getRoles())) {
            commentDao.deleteById(commentId);
            return ResponseResult.SUCCESS("评论删除成功");
        } else {
            return ResponseResult.PERMISSION_DENIED();
        }
    }
}
